package org.bitoy.shimmer.common.enums;

import org.springframework.validation.BindException;

public interface DisplayableEnum {
    String serialize();

    static <T extends DisplayableEnum> T getEnum(Class<T> targetType, String source) throws BindException {
        T target = null;
        for (T e : targetType.getEnumConstants()) {
            if (e.serialize().equals(source)) {
                target = e;
            }
        }
        if (target == null) {
            throw new BindException(source, "枚举类型不存在");
        } else {
            return target;
        }
    }
}
